package org.example.aip;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class StorageService {

    private static Logger logger = LoggerFactory.getLogger(StorageService.class) ;

    //1.采用 Ribbon 的形式
    @Autowired
    private RestTemplate restTemplate ;

    @Autowired
    private StorageServiceFeign storageServiceFeign;

    //2.采用 Feign 的形式
    public void deduct(String commodityCode, int orderCount) {

//        ResponseEntity<Void> entity = restTemplate. getForEntity( "http://storage-service/deduct/{commodityCode}/{orderCount}", Void.class, commodityCode, orderCount );
//        if (entity.getStatusCode()== HttpStatus.OK){
//            logger.info("扣减库存成功，商品编号为{}，本次扣减的数量为{}",commodityCode,orderCount);
//            return;
//        }
//        throw new RuntimeException("扣减库存失败") ;

        ResponseEntity<Void> entity = storageServiceFeign.deduct(commodityCode, orderCount);
        if (entity.getStatusCode() == HttpStatus.OK) {
            logger.info("扣减库存成功，商品编号为{}，本次扣减的数量为{}", commodityCode, orderCount);
            return;
        }
        throw new RuntimeException("扣减库存失败");
    }
}
